//
//  TabBarController.m
//  jingjing2.0
//
//  Created by apple on 16/4/27.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "TabBarController.h"
@interface TabBarController ()<UITabBarControllerDelegate>

@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectedIndex=0;
    self.delegate=self;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.buttomView];
    [self setUpChildController];
    
}

//底部视图创建
-(UIImageView *)buttomView
{
    if (!_buttomView) {
        _buttomView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tab_bg1"]];
        _buttomView.frame=self.tabBar.frame;
        _buttomView.tag=66;
        
    }
    return _buttomView;
}
//创建子视图
-(void)setUpChildController
{
    HomeViewController *home = [[HomeViewController alloc]init];
    
    UINavigationController *vc1 = [[UINavigationController alloc]initWithRootViewController:home];
    vc1.view.tag=1;
    
    FuLiViewController *fuli = [[FuLiViewController alloc]init];
    UINavigationController *vc2 = [[UINavigationController alloc]initWithRootViewController:fuli];
    vc2.view.tag=2;
    
    OrderViewController*order = [[OrderViewController alloc]init];
    UINavigationController *vc3 = [[UINavigationController alloc]initWithRootViewController:order];
    vc3.view.tag=3;
    
    MyInfoViewController*info = [[MyInfoViewController alloc]init];
    UINavigationController *vc4 = [[UINavigationController alloc]initWithRootViewController:info];
    vc4.view.tag=4;
    
    [self setViewControllers:@[vc1,vc2,vc3,vc4]];
    
    //添加滑动手势左
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedRightButton:)];
    
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    
    [self.view addGestureRecognizer:swipeLeft];
    
    
    //添加滑动手势右
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedLeftButton:)];
    
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    
    [self.view addGestureRecognizer:swipeRight];

}

//切换图片
 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (self.selectedViewController==viewController) {
        switch (viewController.view.tag) {
            case 1:
                self.buttomView.image=[UIImage imageNamed:@"tab_bg1"];
                break;
            case 2:
                self.buttomView.image=[UIImage imageNamed:@"tab_bg2"];
                break;
            case 3:
                self.buttomView.image=[UIImage imageNamed:@"tab_bg3"];
                break;
            case 4:
                self.buttomView.image=[UIImage imageNamed:@"tab_bg4"];
                break;
            default:
                break;
        }
    }
   
}
//手势方法
#pragma mark---手势操作
- (void) tappedRightButton:(id)sender

{
    NSUInteger selectedIndex = self.selectedIndex;

    NSArray *aryViewController = self.viewControllers;
    
    if (selectedIndex < aryViewController.count - 1) {
        
        UIView *fromView = [self.selectedViewController view];
        
        UIView *toView = [[self.viewControllers objectAtIndex:selectedIndex + 1] view];
        self.buttomView.image=[UIImage imageNamed:[NSString stringWithFormat:@"tab_bg%lu",selectedIndex+2]];
        [UIView transitionFromView:fromView toView:toView duration:0.8f options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
            
            if (finished) {
                
                [self setSelectedIndex:selectedIndex + 1];
                
            }
            
        }];
        
    }
    
    
    
}
- (void) tappedLeftButton:(id)sender

{
    
    NSUInteger selectedIndex = [self selectedIndex];
    if (selectedIndex > 0) {
        
        UIView *fromView = [self.selectedViewController view];
        
        UIView *toView = [[self.viewControllers objectAtIndex:selectedIndex - 1] view];
        self.buttomView.image=[UIImage imageNamed:[NSString stringWithFormat:@"tab_bg%lu",(unsigned long)selectedIndex]];
        [UIView transitionFromView:fromView toView:toView duration:0.5f options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
            
            if (finished) {
                
                [self setSelectedIndex:selectedIndex - 1];
                
            }
            
        }];
        
    }
    
    
}

-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
-(void)hideTabBar
{
    UIImageView *view=[self.view viewWithTag:66];
    self.tabBar.hidden=YES;
    view.hidden=YES;
    
}
-(void)showTabBar
{
    UIImageView *view=[self.view viewWithTag:66];
    //因为tabBar会覆盖到imageView上面，但又不能隐藏tabBar
    self.tabBar.alpha=0.1;
    self.tabBar.hidden=NO;
    view.hidden=NO;

}
@end
